﻿using MeetingAssistant.Domain.Model.Member;
using MeetingAssistant.ViewModel.Member;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace MeetingAssistant.Domain.Data.Repositories.Member.Impl
{
    public partial class CompanyRepository
    {
        public IList<CompanyVM> GetListCompanyVM(Expression<Func<Company, bool>> wh, int limit, int offset, out int total)
        {
            //当新增公司没有会议负责人时，也能显示公司信息
            //var cList = Context.Companys.Join(Context.Users,
            //                                  c => c.Id,
            //                                  u => u.Company.Id,
            //                                  (c, u) => new { c, u})
            //                                  .Where(cu => cu.u.Roles.)
            //                                  .Select(com => new CompanyVM() {
            //                                      Id = com.c.Id,
            //                                      CompanyName = com.c.CompanyName,
            //                                      CompanyAddress = com.c.CompanyAddress,
            //                                      Code = com.c.Code,
            //                                      UserName = com.u.UserName,
            //                                      LeaderName = com.u.TrueName,
            //                                      LeaderIdentityNumber = com.u.IdentityNumber,
            //                                      LeaderSex = com.u.Sex,
            //                                      LeaderEmail = com.u.Email,
            //                                      LeaderPhone = com.u.Phone,
            //                                      LeaderDepartment = com.u.Department,
            //                                      LeaderAddress = com.u.Address,
            //                                      Enabled = com.c.Enabled,
            //                                      UpdateDate = com.c.UpdateDate
            //                                  });
            //var ta = cList.Count();
            var list = Context.Users.Where(c => c.Roles.Count(c1 => c1.RoleName == "公司负责人") > 0)
                                     .Select(user => new CompanyVM()
                                     {
                                         Id = user.Id,
                                         CompanyName = user.Company.CompanyName,
                                         CompanyAddress = user.Company.CompanyAddress,
                                         Code = user.Company.Code,
                                         UserName = user.UserName,
                                         LeaderName = user.TrueName,
                                         LeaderIdentityNumber = user.IdentityNumber,
                                         LeaderSex = user.Sex,
                                         LeaderEmail = user.Email,
                                         LeaderPhone = user.Phone,
                                         LeaderDepartment = user.Department,
                                         LeaderAddress = user.Address,
                                         Enabled = user.Company.Enabled,
                                         UpdateDate = user.Company.UpdateDate
                                     });
          
            total = list.Count();
            if (offset > 0)
            {
                return list.OrderBy(c => c.Id).ThenBy(c => c.UpdateDate).Skip(offset).Take(limit).ToList();
            }
            return list.ToList();
        }
    }
}
